// pages/homepage/tocategory/tocategory.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: false,
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取登录信息
   /* let userInfo = wx.getStorageSync("userInfo");
    if (null != userInfo && "" != userInfo) {
      this.setData({ userInfo: userInfo });
    }*/

////////
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    var category1Id = options.category1id
    console.log("类型ID", category1Id)
    wx.request({
      url: app.globalData.baseUrl +'/category1/selectcategory1byid',
      method: 'GET',
      data: { "category1Id": options.category1id },//传入的参数
      success: function (res) {
        var list = res.data.category1;
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            category2Items: list
          });
        }
      }
    })

  },
  //////授权
  login: function (e) {
    var that = this
    console.log(e);
    if (e.detail.errMsg.indexOf("ok") != -1) {
      //this.setData({ userInfo: e.detail.userInfo });
      //wx.setStorageSync("userInfo", e.detail.userInfo);//将对象存入本地缓存
      wx.setStorageSync("city", e.detail.userInfo.city);//将地址存入本地缓存
      console.log("这是.....：" + e.detail.userInfo.nickName);
      wx.login({
        success: function (res) {
          console.log("res...code..：" + res.code);
          //var code=res.code;
          wx.request({
            url: app.globalData.baseUrl + '/userinfo/login',
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {//获取数据传到后台
              code: res.code,
              avatarUrl: e.detail.userInfo.avatarUrl,
              userName: e.detail.userInfo.nickName,
              userGender: e.detail.userInfo.gender,
              userCity: e.detail.userInfo.city,
              userProvince: e.detail.userInfo.province,
              userCountry: e.detail.userInfo.country,
            },

            success: function (res) {
              console.log("这是....：" + res.data.userInfo.buyerOpenid);
              const user = res.data.userInfo
              ///
              if (user == null) {
                var toastText = '获取数据失败' + res.data.errMsg;
                wx.showToast({
                  title: toastText,
                  icon: '',
                  duration: 2000
                });
              } else {
                // 将返回的数据保存到全局的缓冲中，方便其他页面使用
                wx.setStorageSync("openid", user.buyerOpenid);//将对象存入本地缓存
                wx.setStorageSync("userInfo", user);//将对象存入本地缓存
                that.setData({ userInfo: user });

              }

              // 将返回的数据保存到全局的缓冲中，方便其他页面使用
              // wx.setStorageSync("openid", openid);//将对象存入本地缓存
            }
          })
        }
      })

    }
  },
//////跳转订单页
  catchTapCategory2: function (e) {
    wx.setStorageSync("flag", false);
    var that = this;
    let openid = wx.getStorageSync("openid");
    console.log('openid:' + openid);
    var category2Id = e.currentTarget.dataset.category2id;
    console.log('category2Id:' + category2Id);
    wx.setStorageSync("category2ToOrder", category2Id);//将类型标号存入本地缓存
    wx.navigateTo({
      url: '../test/test?category2id=' + category2Id + '&openid=' + openid,


    })
    

   
  },

  /*******下拉刷新********* */
  // 页面下拉刷新

  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      orderList: null
    });
    setTimeout(function () {
      that.onShow()
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 1500);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})